# -*- coding: utf-8 -*-
"""
@Time    : 2024/7/4 15:02 
@Author  : ZhangShenao 
@File    : error_handling.py 
@Desc    : 异常处理
使用try-except语句来捕获并处理异常
"""


# 下面是几种常见的异常类型
# 10 / 0  # ZeroDivisionError: division by zero

# a * 2   # NameError: name 'a' is not defined

# 1 + [1, 2]  # TypeError: unsupported operand type(s) for +: 'int' and 'list'

# user = {}
# print(user['name']) # KeyError: 'name'


def print_sum():
    """让用户输入两个数字,打印它们的加和"""

    try:
        text = input('Please input two numbers separated by a comma: \n')
        nums = text.strip()
        a = int(nums[0].strip())
        b = int(nums[1].strip())
        print(f'the sum is {a + b}')
    # 使用多except分支,按照类型捕获异常
    except ValueError as e:
        print(f'value error: {e}')
    except IndexError as e:
        print(f'index error: {e}')
    except Exception as e:  # 最终匹配任意类型异常
        print(f'unknown error: {e}')
    print('continue')


if __name__ == '__main__':
    print_sum()
